<?php
namespace App\Repository\Java;

use App\Model\Shopping;
use App\Repository\Contracts\ShoppingJavaInterface;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-02-06 13:42
 *
 * @Description:
 */
class ShoppingRepository extends Repository implements ShoppingJavaInterface
{
    protected function setPrefix()
    {
        $this->prefix = '';
    }

    public function syncShopping($storeId, array $data)
    {
        if (!is_array($data) || count($data) == 0) {
            return false;
        }
        $form['customerId'] = $data['uid'];
        $form['platform'] = 7;
        foreach ($data['detail'] as $key => $val) {
            $form['salOrderEntryBeanList'][$key]['seq'] = $key + 1;
            $form['salOrderEntryBeanList'][$key]['goodsId'] = $val['goods_id'];
            $form['salOrderEntryBeanList'][$key]['stockPlaceId'] = $storeId;
            $form['salOrderEntryBeanList'][$key]['realQty'] = $val['counts'];
            $form['salOrderEntryBeanList'][$key]['taxPrice'] = $val['price'];
        }
        $result = $this->post('salOrder/saveSalOrder', $form);

        return $result['resultType'] === 'success';
    }

    public function syncRefund($storeId, array $data)
    {
        if (!is_array($data) || count($data) == 0) {
            return false;
        }
        $form['billCustomerId'] = $data['uid'];
        $form['billRob'] = -1;
        $form['platform'] = 7;
        foreach ($data['detail'] as $key => $val) {
            $form['salOutStockEntryList'][$key]['seq'] = $key + 1;
            $form['salOutStockEntryList'][$key]['goodsId'] = $val['goods_id'];
            $form['salOutStockEntryList'][$key]['stockPlaceId'] = $storeId;
            $form['salOutStockEntryList'][$key]['realQty'] = $val['counts'];
        }
        $result = $this->post('saloutstock/addSaveSalOutStock', $form);

        return $result['resultType'] === 'success';
    }

    public function syncShoppingAmount($storeId, array $data)
    {
        if (!is_array($data) || count($data) == 0) {
            return false;
        }
        $form['billCustomerId'] = $data['uid'];
        $form['billRob'] = 1;
        $key = 0;
        if ($data['card_amount'] > 0) {
            $form['receiptEntryList'][$key]['seq'] = $key + 1;
            $form['receiptEntryList'][$key]['payMethod'] = Shopping::MEMBER_CARD;
            $form['receiptEntryList'][$key]['payAmount'] = $data['card_amount'];
            $key += 1;
        }
        $form['receiptEntryList'][$key]['seq'] = $key + 1;
        $form['receiptEntryList'][$key]['payMethod'] = $data['pay_type'];
        $form['receiptEntryList'][$key]['payAmount'] = $data['amount'];
        $result = $this->post('receipt/addSaveReceipt', $form);

        return $result['resultType'] === 'success';
    }

    public function syncRefundAmount($storeId, array $data)
    {
        if (!is_array($data) || count($data) == 0) {
            return false;
        }
        $form['billCustomerId'] = $data['uid'];
        $form['billRob'] = -1;
        $key = 0;
        if ($data['card_amount'] > 0) {
            $form['receiptEntryList'][$key]['seq'] = $key + 1;
            $form['receiptEntryList'][$key]['payMethod'] = Shopping::MEMBER_CARD;
            $form['receiptEntryList'][$key]['payAmount'] = $data['card_amount'];
            $key += 1;
        }
        $form['receiptEntryList'][$key]['seq'] = $key + 1;
        $form['receiptEntryList'][$key]['payMethod'] = $data['pay_type'];
        $form['receiptEntryList'][$key]['payAmount'] = $data['amount'];
        $result = $this->post('receipt/addSaveReceipt', $form);

        return $result['resultType'] === 'success';
    }
}